<template>
  <el-container style="margin: 0 50px">
    <el-header>
      <el-menu
        :default-active="activeIndex"
        :ellipsis="false"
        mode="horizontal"
      >
        <div class="logo">
          <img style="width: 50px" src="../assets/logo.png" alt="logo" />
        </div>
        <div class="flex-grow" />
        <el-menu-item index="1" @click="navigate('/')">首页</el-menu-item>
        <el-menu-item index="2" @click="navigate('/commlist')"
          >商品列表</el-menu-item
        >
        <el-menu-item index="3" @click="navigate('/shopping-cart')"
          >购物车</el-menu-item
        >
        <el-menu-item index="4" @click="navigate('/order-list')"
          >订单列表</el-menu-item
        >
        <div v-if="!user.username" style="width: 150px; padding-top: 12px">
          <el-button @click="router.push('/login')">登录</el-button>
          <el-button @click="router.push('/register')">注册</el-button>
        </div>
        <div v-else>
          <el-dropdown style="margin-top: 5px;">
            <span class="el-dropdown-link">
              <el-avatar
                shape="square"
                :size="40"
                :src="$apiImg+user.avatarUrl"
                style="border-radius: 50%"
              >
              </el-avatar>
            </span>
            <template #dropdown>
              <el-dropdown-menu tabindex="1">
                <el-dropdown-item @click="editRow">个人信息</el-dropdown-item>
                <el-dropdown-item @click="addressList">收货地址</el-dropdown-item>
                <el-dropdown-item @click="loginOut">退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-menu>
    </el-header>
  </el-container>
  <el-dialog v-model="dialogFormEVisible" title="个人信息" :lock-scroll="false">
    <el-form
      ref="userForm"
      :model="formData"
      label-position="right"
      label-width="80px"
    >
      <el-upload
        class="avatar-uploader"
        :action="$apiImg+'/user/uploadAvatar?username=' + formData.username"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
      >
        <img
          v-if="formData.avatarUrl"
          :src="$apiImg+formData.avatarUrl"
          class="avatar"
        />
        <el-icon class="el-icon avatar-uploader-icon" v-else><UploadFilled /></el-icon>
      </el-upload>
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="formData.username"
          disabled
          placeholder="请输入用户名"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" placeholder="请输入用户邮箱" />
      </el-form-item>
      <el-form-item label="昵称" prop="email">
        <el-input v-model="formData.nickname" placeholder="请输入用户昵称" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入用户手机号" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="formData.password"
          placeholder="请输入用户密码"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即保存</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
  <el-drawer
    v-model="table"
    title="收货地址"
    direction="ltr"
    size="50%"
    :lock-scroll="false"
  >
  <el-button type="primary" @click="addAdress">新增</el-button>
    <el-table :data="addressData" style="width: 100%;height:100%;">
      <el-table-column property="street" label="地址"/>
      <el-table-column property="detail" label="详细地址"/>
      <el-table-column property="zipCode" label="邮编" />
      <el-table-column property="recipient" label="收件人" />
      <el-table-column property="rePhone" label="手机号" />
      <el-table-column label="操作" width="150px">
  <template v-slot="scope">
    <el-button type="success" size="small" @click="editAddress(scope.$index)">编辑</el-button>
    <el-button type="danger" size="small" @click="delAddress(scope.$index)">删除</el-button>
  </template>
</el-table-column>
    </el-table>
  </el-drawer>
  <el-dialog v-model="dialogVisible" title="添加/修改收货地址" :lock-scroll="false">
    <el-form :model="editadrsData" label-width="100px">
      <el-form-item label="地址">
        <el-input v-model="editadrsData.street"></el-input>
      </el-form-item>
      <el-form-item label="详细地址">
        <el-input v-model="editadrsData.detail"></el-input>
      </el-form-item>
      <el-form-item label="邮编">
        <el-input v-model="editadrsData.zipCode"></el-input>
      </el-form-item>
      <el-form-item label="收件人">
        <el-input v-model="editadrsData.recipient"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="editadrsData.rePhone"></el-input>
      </el-form-item>
      <el-form-item>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="saveAddress()">保存</el-button>
    </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { reactive, toRefs  } from 'vue'
import { userApi, saveApi, getAdrApi, updateAdrApi, delAdrApi } from '@/api/user'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import { outApi } from '@/api/login'
export default {
  props: {
    activeIndex: {
      type: String,
      required: true
    }
  },
  setup() {
    const userLogin = reactive({
      user: {},
      dialogFormEVisible: false,
      dialogVisible: false,
      addressData: [],
      editadrsData: {},
      table: false,
      formData: {}
    })
    const store = useStore()
    const router = useRouter()
    const uInfo = store.state.uInfo.userInfo
    userLogin.user.username = uInfo.username
    const userDetail = () => {
      if (userLogin.user.username != undefined) {
        userApi(userLogin.user).then((res) => {
          if (res) {
            userLogin.user = res.data
          } else {
            return null
          }
        })
      }
    }
    const loginOut = () => {
      outApi(userLogin.user).then((res) => {
        if (res) {
          localStorage.removeItem('loginData')
          store.commit('setUserInfo', {})
          ElMessage({
            message: '退出成功',
            type: 'success',
            duration: 2000
          })
          router.go(0)
        }
      })
    }
    const Addressfind = () => {
      getAdrApi(userLogin.user).then((res) => {
        if(res){
          userLogin.addressData = res.data;
        }
      })
    }
    const editRow = () => {
      userLogin.dialogFormEVisible = true
      userLogin.formData = userLogin.user
    }
    const addressList = () => {
      userLogin.table = true
      Addressfind()
    }
    const saveAddress=()=>{
        updateAdrApi(userLogin.editadrsData).then((res)=>{
          if(res){
          if(!userLogin.editadrsData.id){
            userLogin.addressData.push(res.data)
          }else{
            const ids = userLogin.addressData.findIndex((a) => a.id === userLogin.editadrsData.id)
            userLogin.addressData[ids]=res.data
          }
        }
      })
        userLogin.dialogVisible = false;
    }
    const delAddress=(index)=> {
      delAdrApi({delId:userLogin.addressData[index].id}).then((res)=>{
        if(res&&(userLogin.addressData.splice(index, 1))){
          ElMessage({
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        }
      })
    }
    const onSubmit = () => {
      saveApi(userLogin.user).then((detail) => {
        if (detail.data) {
          ElMessage({
            message: '修改成功',
            type: 'success',
            duration: 2000
          })
        } else {
          return null
        }
      })
    }
    userDetail()
    return {
      ...toRefs(userLogin),
      loginOut,
      router,
      editRow,
      onSubmit,
      uInfo,
      addressList,
      saveAddress,
      delAddress
    }
  },
  methods: {
    navigate(path) {
      this.router.push(path)
    },
    handleAvatarSuccess(res) {
      if (res) {
        ElMessage({
          message: '修改成功',
          type: 'success',
          duration: 2000
        })
        this.formData.avatarUrl = res.data.avatarUrl
      }
    },
    addAdress() {
      this.dialogVisible = true;
      this.editadrsData = {userId: this.user.id};
    },
    editAddress(index) {
      this.dialogVisible = true;
      this.editadrsData={...this.addressData[index]};
    },
  }
}
</script>
<style scoped>
.flex-grow {
  flex-grow: 1;
}
.el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}
.el-dropdown-link:focus {
  outline: 0;
}
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #000000;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  border-radius: 50%;
  display: block;
}
</style>
